package com.codinghero.example.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CliAntExample {

	/**
	 * Options: 
  -help                  print this message
  -projecthelp           print project help information
  -version               print the version information and exit
  -quiet                 be extra quiet
  -verbose               be extra verbose
  -debug                 print debugging information
  -emacs                 produce logging information without adornments
  -logfile <file>        use given file for log
  -logger <classname>    the class which is to perform logging
  -listener <classname>  add an instance of class as a project listener
  -buildfile <file>      use given buildfile
  -D<property>=<value>   use value for given property
  -find <file>           search for buildfile towards the root of the
                         filesystem and use it
	 * 
	 * @param args
	 */
	@SuppressWarnings("static-access")
	private static CommandLine parseArgs(String[] args) {
		// boolean arg
		Option help = new Option( "help", "print this message" );
		Option projecthelp = new Option( "projecthelp", "print project help information" );
		Option version = new Option( "version", "print the version information and exit" );
		Option quiet = new Option( "quiet", "be extra quiet" );
		Option verbose = new Option( "verbose", "be extra verbose" );
		Option debug = new Option( "debug", "print debugging information" );
		Option emacs = new Option( "emacs",
								   "produce logging information without adornments" );
		// other arg
		Option logfile   = OptionBuilder.withArgName( "file" ).hasArg()
				.withDescription(  "use given file for log" )
				.create( "logfile" );
		Option logger    = OptionBuilder.withArgName( "classname" ).hasArg()
				.withDescription( "the class which it to perform logging" )
				.create( "logger" );
		Option listener  = OptionBuilder.withArgName( "classname" ).hasArg()
				.withDescription( "add an instance of class as a project listener" )
				.create( "listener"); 
		Option buildfile = OptionBuilder.withArgName( "file" ).hasArg()
				.withDescription(  "use given buildfile" )
				.create( "buildfile");
		Option find = OptionBuilder.withArgName( "file" ).hasArg()
				.withDescription( "search for buildfile towards the root of the filesystem and use it" )
				.create( "find" );
		// java property option
		Option property  = OptionBuilder.withArgName( "property=value" ).hasArgs(2)
                .withValueSeparator()
                .withDescription( "use value for given property" )
                .create( "D" );
		
		
		// parse
		Options options = new Options();
		options.addOption( help );
		options.addOption( projecthelp );
		options.addOption( version );
		options.addOption( quiet );
		options.addOption( verbose );
		options.addOption( debug );
		options.addOption( emacs );
		options.addOption( logfile );
		options.addOption( logger );
		options.addOption( listener );
		options.addOption( buildfile );
		options.addOption( find );
		options.addOption( property );
		
		CommandLineParser parser = new GnuParser();
		try {
			CommandLine cmd = parser.parse(options, args);
			if (cmd.getOptions() == null || cmd.getOptions().length == 0) {
				HelpFormatter formatter = new HelpFormatter();
				formatter.printHelp("ant", options);
			}
			return cmd;
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
	public static void main(String[] args) {
		CommandLine cmd = parseArgs(args);
		if (cmd.hasOption("buildfile")) {
			System.out.println(cmd.getOptionValue("buildfile"));
		}
		// ......
	}

}
